<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/make_function.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../function_invocation_and_creation.html" title="Chapter 4. Function Invocation and Creation">
<link rel="prev" href="boost_python_data_members_hpp.html" title="boost/python/data_members.hpp">
<link rel="next" href="boost_python_overloads_hpp.html" title="boost/python/overloads.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_data_members_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_overloads_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="function_invocation_and_creation.boost_python_make_function_hpp"></a><a class="link" href="boost_python_make_function_hpp.html" title="boost/python/make_function.hpp">boost/python/make_function.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.functions">Functions</a></span></dt>
<dt><span class="section"><a href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_make_function_hpp.introduction"></a><a class="link" href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          make_function() and make_constructor() are the functions used internally
          by def() and class_&lt;&gt;::def() to produce Python callable objects which
          wrap C++ functions and member functions.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_make_function_hpp.functions"></a><a class="link" href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.functions" title="Functions">Functions</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">make_function</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">)</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">make_function</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">policies</span><span class="special">)</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">KeywordsOrSignature</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">make_function</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">policies</span><span class="special">,</span> <span class="identifier">KeywordsOrSignature</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">ks</span><span class="special">)</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Keywords</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Signature</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">make_function</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">policies</span><span class="special">,</span> <span class="identifier">Keywords</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">kw</span><span class="special">,</span> <span class="identifier">Signature</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">sig</span><span class="special">)</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                F is a function pointer or member function pointer type. If policies
                are supplied, it must be a model of CallPolicies. If kewords are
                supplied, it must be the result of a keyword-expression specifying
                no more arguments than the arity of f.
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd>
<p>
                Creates a Python callable object which, when called from Python,
                converts its arguments to C++ and calls f. If F is a pointer-to-member-function
                type, the target object of the function call (*this) will be taken
                from the first Python argument, and subsequent Python arguments will
                be used as the arguments to f.
              </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                    If policies are supplied, it will be applied to the function
                    as described here.
                  </li>
<li class="listitem">
                    If keywords are supplied, the keywords will be applied in order
                    to the final arguments of the resulting function.
                  </li>
<li class="listitem">
                    If Signature is supplied, it should be an instance of an MPL
                    front-extensible sequence representing the function's return
                    type followed by its argument types. Pass a Signature when wrapping
                    function object types whose signatures can't be deduced, or when
                    you wish to override the types which will be passed to the wrapped
                    function.
                  </li>
</ul></div>
</dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                An instance of object which holds the new Python callable object.
              </p></dd>
<dt><span class="term">Caveats</span></dt>
<dd><p>
                An argument of pointer type may be 0 if None is passed from Python.
                An argument type which is a constant reference may refer to a temporary
                which was created from the Python object for just the duration of
                the call to the wrapped function, for example a std::vector conjured
                up by the conversion process from a Python list. Use a non-const
                reference argument when a persistent lvalue is required.
              </p></dd>
</dl>
</div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">make_constructor</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">)</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">make_constructor</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">policies</span><span class="special">)</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">KeywordsOrSignature</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">make_constructor</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">policies</span><span class="special">,</span> <span class="identifier">KeywordsOrSignature</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">ks</span><span class="special">)</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Keywords</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Signature</span><span class="special">&gt;</span>
<span class="identifier">object</span> <span class="identifier">make_constructor</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">policies</span><span class="special">,</span> <span class="identifier">Keywords</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">kw</span><span class="special">,</span> <span class="identifier">Signature</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">sig</span><span class="special">)</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                F is a function pointer type. If policies are supplied, it must be
                a model of CallPolicies. If kewords are supplied, it must be the
                result of a keyword-expression specifying no more arguments than
                the arity of f.
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd><p>
                Creates a Python callable object which, when called from Python,
                converts its arguments to C++ and calls f.
              </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                An instance of object which holds the new Python callable object.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_make_function_hpp.example"></a><a class="link" href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          C++ function exposed below returns a callable object wrapping one of two
          functions.
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">make_function</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">foo</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="string">"foo"</span><span class="special">;</span> <span class="special">}</span>
<span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">bar</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="string">"bar"</span><span class="special">;</span> <span class="special">}</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="identifier">object</span> <span class="identifier">choose_function</span><span class="special">(</span><span class="keyword">bool</span> <span class="identifier">selector</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">if</span> <span class="special">(</span><span class="identifier">selector</span><span class="special">)</span>
        <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">make_function</span><span class="special">(</span><span class="identifier">foo</span><span class="special">);</span>
    <span class="keyword">else</span>
        <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">make_function</span><span class="special">(</span><span class="identifier">bar</span><span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">make_function_test</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">def</span><span class="special">(</span><span class="string">"choose_function"</span><span class="special">,</span> <span class="identifier">choose_function</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
          It can be used this way in Python:
        </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">from</span> <span class="identifier">make_function_test</span> <span class="identifier">import</span> <span class="special">*</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">choose_function</span><span class="special">(</span><span class="number">1</span><span class="special">)</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">g</span> <span class="special">=</span> <span class="identifier">choose_function</span><span class="special">(</span><span class="number">0</span><span class="special">)</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">f</span><span class="special">()</span>
<span class="char">'foo'</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">g</span><span class="special">()</span>
<span class="char">'bar'</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_data_members_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_overloads_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
